layui.config({
    base: '../res/static/js/util/' //你存放新模块的目录，注意，不是layui的模块目录
}).use(['jquery','cookie'],function(){
	var $ = layui.jquery;
	var cookie = layui.cookie;
	
	//加载登录用户信息
	var user = sessionStorage.getItem("users");
	if(user==null){//如果用户没有登录，跳转到登录页面
		location.href = "login.html";
	}
	user = JSON.parse(user);//将sessionStorage中存储的json字符串解析成json对象
	//console.log(user)
	$(".login").html('欢迎，<span style="font-size:20px ">'+user.nickname+'</span>&nbsp;&nbsp;&nbsp;&nbsp;<a id="logout" href="javascript:;">安全退出</a>');
	
	//退出按钮点击事件
	$("#logout").click(function(){
		$.cookie('token',null);//清除cookie中的token
		sessionStorage.removeItem("users");//删除session
		location.href="login.html";//跳转到登录页
	});
	
	//加载购物车商品种数
	$.post(
		"shopcart/getCount.do?uid="+users.id,
		function(res){
			$(".sp-cart span").html(res);
		},"json"
	);
	//加载今日必抢
	$.post(
		"../../goods/findToday.do",
		function(res){
			var html = '<div class="item-box">';
			$(res).each(function(index){
				var goods_name = this.goods_name.length>8?this.goods_name.substr(0,12)+"...":this.goods_name;
				var delPrice = (this.price*1.6).toFixed(2);//保留2位小数
				html+='<div class="item"><a href="details.html?goods_id='+this.goods_id+'"><img src="/file/'+this.priture+'"></a><div class="title">'+goods_name+'</div><div class="price"><span>￥'+this.price+'</span><del>￥'+delPrice+'</del></div></div>';
				if(index%4==3){
					html+='</div>';
					html+='<div class="item-box">';
				}
			});
			html+='</div>';
			$("#today").append($(html));
		},"json"
	);
});


